//
//  VisitorLoginView.swift
//  sinazd
//
//  Created by apple on 16/3/6.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit


//声明协议
@objc protocol VisitorLoginViewDelegate:NSObjectProtocol{
    
    optional func userWillLogin()
    optional func userWillRegister()
    
    
}


class VisitorLoginView: UIView {
    
    //设置代理
    weak var visitorLoginViewDelegate : VisitorLoginViewDelegate?
    
    
    

  //自定义视图，先找到入口。当加载view，势必调用init函数
    override init(frame: CGRect) {
        super.init(frame:frame)
        
       
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //设置页面信息，通过传入参数，实现各个控制器界面不同
    func setupInfoWith(tipText:String,imgName:String?){
        
        tipLabel.text = tipText
        if imgName != nil {
            
            //一定不是首页
            circleIcon.image = UIImage(named: imgName!)
            
            largeHouse.hidden = true
            
            //把circleIcon移动到最顶层
            self.bringSubviewToFront(circleIcon)
            
        }else{
            
            //一定是在首页 则执行动画
            self.startAnimation()
        }
        
    }
    //动画
    private func startAnimation(){
        
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画的重复次数、旋转角度、完成一次动画的时间
        ani.repeatCount = MAXFLOAT
        ani.toValue = 2 * M_PI
        ani.duration = 20
        //设置动画完成或者处于非活跃状态时不移除动画
        ani.removedOnCompletion = false
        
        //添加基础动画到图层
        circleIcon.layer.addAnimation(ani, forKey: nil)
        
        
    }
    
    //设置登录的界面
    private func setupUI(){
        
        addSubview(circleIcon)
        addSubview(mask)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        
        //使用第三方框架设置autolayout布局
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(230)
            make.height.equalTo(36)
        }
        
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.right.equalTo(tipLabel.snp_right)
            make.width.equalTo(100)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.left.equalTo(tipLabel.snp_left)
            make.width.equalTo(100)
        }
        
        mask.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        
        
        //设置view的背景颜色和那个遮挡circleItem的图片颜色一致
        let value:CGFloat = 236.0 / 255.0
        self.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        //设置登录和注册按钮的点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    
    //按钮的点击事件
    @objc private func loginBtnDidClick(){
        
        visitorLoginViewDelegate?.userWillLogin?()
        
        
    }
    
    @objc private func registerBtnDidClick(){
        
        visitorLoginViewDelegate?.userWillRegister?()
        
    }
    
    private lazy var mask:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //懒访客界面上的控件
    private lazy var circleIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var largeHouse:UIImageView = {
       
        let iv = UIImageView()
        iv.image = UIImage(named: "visitordiscover_feed_image_house")
        return iv
 
    }()
    
    //private lazy var tipLabel:UILabel = UILabel.labelWith("关注一些人，回到这里看看有什么惊喜", textColor: UIColor.darkGrayColor(), fontSize: 14)
    private lazy var tipLabel:UILabel = UIFactory.labelWith("关注一些人，回到这里看看有什么惊喜", textColor: UIColor.darkGrayColor(), fontSize: 14)
  
    
    //登录和注册按钮
    private lazy var loginBtn: UIButton = UIFactory.buttonWithTitle("登录", backImageName: "common_button_white_disable", textColor: UIColor.darkGrayColor(), fontSize: 14)
    
  private lazy var registerBtn:UIButton = UIFactory.buttonWithTitle("注册", backImageName: "common_button_white_disable", textColor: UIColor.orangeColor(), fontSize: 14)
    
  

}
